(function () {
    function parseURL(url) {
        var a = document.createElement('a');
        a.href = url;
        return {
            source: url,
            protocol: a.protocol.replace(':', ''),
            host: a.hostname,
            port: a.port,
            query: a.search,
            params: (function () {
                var ret = {},
                    seg = a.search.replace(/^\?/, '').split('&'),
                    len = seg.length,
                    i = 0,
                    s;
                for (; i < len; i++) {
                    if (!seg[i]) {
                        continue;
                    }
                    s = seg[i].split('=');
                    ret[s[0]] = s[1];
                }
                return ret;
            })()
        }
    }

    var referrer = parseURL(document.referrer);
    var documentURI = parseURL(document.documentURI);
    var oldReferrer = util.cookie.get('track_referrer');
    if (((oldReferrer != null && referrer.source != oldReferrer) || oldReferrer == null) && referrer.host != documentURI.host) {
        util.cookie.set('track_referrer', referrer.source, 7 * 24 * 3600)
        util.cookie.set('track_index', document.documentURI, 7 * 24 * 3600)
        util.cookie.set('track_referrer_time', new Date().format('Y-m-d H:i:s'), 7 * 24 * 3600);
        util.cookie.del('track_bd_vid');
        util.cookie.del('track_key1');
        util.cookie.del('track_key2');
        util.cookie.del('track_key3');
        util.cookie.del('track_code');
        if (documentURI.params.bd_vid) {
            util.cookie.set('track_bd_vid', documentURI.params.bd_vid, 7 * 24 * 3600);
        }
        if (documentURI.params.key1) {
            util.cookie.set('track_key1', documentURI.params.key1, 7 * 24 * 3600);
            util.cookie.del('track_key2');
            util.cookie.del('track_key3');
        }
        if (documentURI.params.key2) {
            util.cookie.set('track_key2', documentURI.params.key2, 7 * 24 * 3600);
        }
        if (documentURI.params.key3) {
            util.cookie.set('track_key3', documentURI.params.key3, 7 * 24 * 3600);
        }
        if (documentURI.params.track_code) {
            util.cookie.set('track_code', documentURI.params.track_code, 7 * 24 * 3600);
        }
    }

    /*var logger = new window.Tracker('mcake-log.cn-hangzhou.log.aliyuncs.com', 'mcake-log', 'new-web-tracking');

    logger.push('url', document.documentURI);
    logger.push('title', document.title);
    logger.push('type', 'page');
    logger.push('client', 'pc');
    logger.logger();*/

    return console.log('tracking...');
})();